home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!not-for-mail
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: Operator overload problem!
- Date: 19 Mar 1996 18:12:50 GMT
- Organization: Clark Internet Services, Inc., Ellicott City, MD USA
- Message-ID: <4imtf2$520@clarknet.clark.net>
- References: <DoIn1z.Gou@latcs1.lat.oz.au>
- NNTP-Posting-Host: explorer.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- I'm not sure what's causing your particular syntax error messages, but
- your << overload function promises to return an ostream& and then doesn't
- return anything.
-
- Did you #include <iostream.h>?
-
- Gregary J Boyles (boylesgj@lion.cs.latrobe.edu.au) wrote:
- : I am trying to overload the << but I get the syntax errors : friends must be functions or
- : classes, 'ostream' cannot start a parameter declaration, 'Address::operator <<(int &,Address &)'
- : must be declared at the first set of astericks and declaration syntax error at the second set.
- :
- : What the #$%& is it on about? I copied the operator overload function directly out of a program
- : which compiles and runs so why all of a sudden won't the bloody compiler accept it?
- :
- :
- : #include "defines.h"
- :
- : class Address
- : {
- : private : int Number;
- : char *Street;
- : char *City;
- : int Zip;
- :
- : public : // Constructors
- : Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- : Address();
- : // Copy constructor
- : Address(Address& AnAddress);
- : // Deconstructor
- : ~Address();
- : // Operator overloads
- :
- : **************************************************************************
- : * *
- : * friend ostream& operator <<(ostream& OutPutStream,Address& AnAddress); *
- : * *
- : **************************************************************************
- :
- : // Functions
- : void Change(int ANumber,const char *AStreet,const char *ACity,int AZip);
- : };
- :
- : #endif
- :
- :
- :
- :
- :
- :
- :
- : // address.cpp
- :
- : #include "address.h"
- : #include <string.h>
- :
- : // Constructors
- : Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- : {
- : Number=ANumber;
- : Street=new char[strlen(AStreet)+1];
- : strcpy(Street,AStreet);
- : City=new char[strlen(ACity)+1];
- : strcpy(City,ACity);
- : Zip=AZip;
- : }
- :
- : Address::Address()
- : {
- : Number=0;
- : Street=new char[strlen("")+1];
- : strcpy(Street,"");
- : City=new char[strlen("")+1];
- : strcpy(City,"");
- : Zip=0;
- : }
- :
- : // Copy constructor
- : Address::Address(Address& AnAddress)
- : {
- : Number=AnAddress.Number;
- : Street=new char[strlen(AnAddress.Street)+1];
- : strcpy(Street,AnAddress.Street);
- : City=new char[strlen(AnAddress.City)+1];
- : strcpy(City,AnAddress.City);
- : Zip=AnAddress.Zip;
- : }
- :
- : // Deconstructor
- : Address::~Address()
- : {
- : Number=0;
- : strcpy(Street,"");
- : strcpy(City,"");
- : Zip=0;
- : }
- :
- : // Operator overloads
- :
- : ***************************************************************
- : *
- : ostream& operator <<(ostream& OutPutStream,Address& AnAddress)*
- : *
- : ***************************************************************
- :
- : {
- : OutPutStream<<"Street : "<<AnAddress.Number<<" "<<AnAddress.Street<<EOLN;
- : OutPutStream<<"City : "<<AnAddress.City<<EOLN;
- : OutPutStream<<"Zip code : "<<AnAddress.Zip<<EOLN;
- : }
- :
- : // Functions
- : void Address::Change(int ANumber,const char *AStreet,const char *ACity,int AZip)
- : {
- : Number=ANumber;
- : delete Street;
- : Street=new char[strlen(AStreet)+1];
- : strcpy(Street,AStreet);
- : delete City;
- : City=new char[strlen(ACity)+1];
- : strcpy(City,ACity);
- : Zip=AZip;
- : }
- :
-